package com.acc.store.service.impl;

import com.acc.store.entity.Category;
import com.acc.store.entity.Product;
import com.acc.store.entity.ShopCar;
import com.acc.store.mapper.ShopCarMapper;
import com.acc.store.service.ShopCarService;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 10067
 * @description 针对表【shop_car】的数据库操作Service实现
 * @createDate 2023-10-17 22:55:04
 */
@Service
@Slf4j
public class ShopCarServiceImpl extends ServiceImpl<ShopCarMapper, ShopCar>
        implements ShopCarService {

    @Override
    public List<ShopCar> getList(Long orderId) {

        List<ShopCar> list = lambdaQuery().eq(ShopCar::getOrderId, orderId).list();
        return list;
    }

    @Override
    @Transactional
    public void addr(Long orderId, Long productId) {
        List<ShopCar> shopCarList = lambdaQuery().eq(ShopCar::getOrderId, orderId).eq(ShopCar::getProductId, productId).list();

        if (shopCarList != null && !shopCarList.isEmpty()) {
            log.info("商品数量加1");
            for (ShopCar shopCar : shopCarList) {
                if (shopCar.getProductId().equals(productId)) {
                    shopCar.setNum(shopCar.getNum() + 1);
                }
            }
            this.updateBatchById(shopCarList);
        } else {
            ShopCar shopCar = new ShopCar();
            shopCar.setProductId(productId);
            shopCar.setNum(1);
            Product product = Db.getById(productId, Product.class);
            shopCar.setPrice(product.getPrice());
            shopCar.setProductName(product.getName());
            shopCar.setOrderId(orderId);
            save(shopCar);
        }
    }

    @Override
    @Transactional
    public void removeByOrderIds(List<Long> orderIds) {
        List<ShopCar> list = new ArrayList<>();
        for (Long orderId : orderIds) {
            list.addAll(getList(orderId));
        }
        removeByIds(list);
    }

    @Override
    public void num(Long id, Integer num) {
        ShopCar byId = getById(id);
        byId.setNum(num);
        updateById(byId);
    }

    @Override
    public Map<String, Integer> pie() {
        //todo 这里将查询出所有的购物车 （包括已支付和未支付的）
        List<ShopCar> list = lambdaQuery().list();
        Map<String, Integer> map = new HashMap<>();
        for (ShopCar shopCar : list) {
            Product product = Db.getById(shopCar.getProductId(), Product.class);
            Category category = Db.getById(product.getCategoryId(), Category.class);
            if(map.get(category.getCategoryName())==null){
                map.put(category.getCategoryName(),1);
            }else {
                map.put(category.getCategoryName(),map.get(category.getCategoryName())+1);
            }
        }
        return map;
    }
}




